﻿using HttpCode.Core;
using HttpService;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using ResultModel.Web.MSDN;
using HtmlAgilityPack;
using DataSave.Web.MSDN;

namespace QueryPath.Web.MSDN
{
    public class WebMSDN : IWebMSDN
    {
        private readonly IReptileService _reptileService;
        private readonly IForRedisService _forRedisService;

        public WebMSDN(IReptileService ReptileService, IForRedisService ForRedisService)
        {
            _reptileService = ReptileService;
            _forRedisService = ForRedisService;
        }
        public async Task GetMSDNAllDataAsync()
        {
          
            Console.WriteLine("开始爬取MSDN");
            Console.WriteLine("获取所有分类");
            var topCate =  await GetFirstCategory();
            await _forRedisService.SaveCateAsync(topCate);
        }
        private async Task<List<Category>> GetFirstCategory()
        {
            List<Category> categoryList = new List<Category>();
            HttpItems items = new HttpItems
            {
                Url = @"https://msdn.itellyou.cn/",
                Method = "get",
                ContentType = "text/html",
            };
            var webResult = await _reptileService.GetResponseAsync(items);
            //解析数据
            HtmlDocument doc = new HtmlDocument();
            //加载html
            doc.LoadHtml(webResult.Html);
            HtmlNodeCollection itemNodes = doc.DocumentNode.SelectNodes("/html/body/div[1]/div[1]/div[1]/div[1]/div");
            if (itemNodes != null)
            {
                foreach (var item in itemNodes)
                {
                    Category category = new Category() { }; 
                    var obj = item.SelectSingleNode("div[1]/h4/a");
                    category.CategoryId = obj?.Attributes["data-menuid"]?.Value;
                    category.Name = obj.InnerText;
                    category.Level = 1;
                    categoryList.Add(category);
                }
            }
            return categoryList;
        }
    }
}
